﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SocketControl.Protocol.Http.Expand
{
    public static class TypeExpand
    {

        /// <summary>
        /// 判断字符串是否为空;当对象为Null或则值为string.empty返回true
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static bool IsNull(this string args)
        {
            if (args == null || args == string.Empty)
            {
                return true;
            }
            return false;
        }

        public static IEnumerable<string> ReadLine(this string args)
        {
            int offset = 0;
            int pos = 0;
            char[] buff = args.ToCharArray();
            while (offset < args.Length)
            {
                pos = args.IndexOf("\r\n", offset);
                if (pos < args.Length - 2)
                {
                    yield return new string(buff, offset, pos - offset);
                }
                offset = pos + 2;
            }
        }

        /// <summary>
        /// 当前字符串之后拼接新的字符串，带换行
        /// </summary>
        /// <param name="args"></param>
        /// <param name="str">需要拼接的字符串</param>
        /// <returns></returns>
        public static void JoinAfterWithCRLF(this string args, string str, out string result)
        {
            result = args + str + "\r\n";
        }

        /// <summary>
        /// 当前字符串之前拼接新的字符串，带换行
        /// </summary>
        /// <param name="args"></param>
        /// <param name="str">需要拼接的字符串</param>
        /// <returns></returns>
        public static void JoinBeforeWithCRLF(this string args, string str, out string result)
        {
            result = str + "\r\n" + args;
        }

        /// <summary>
        /// 当前字符串之后拼接新的字符串，带空格
        /// </summary>
        /// <param name="args"></param>
        /// <param name="str">需要拼接的字符串</param>
        /// <returns></returns>
        public static void JoinAfterWithSpace(this string args, string str, out string result)
        {
            result = args + str + " ";
        }

        public static IList<Model.Item> Analysis(this string args)
        {
            IList<Model.Item> result = new List<Model.Item>();
            if (!args.IsNull())
                foreach (string o in args.Split(';'))
                {
                    if (!o.IsNull())
                    {
                        string[] str = o.Split('=');
                        result.Add(
                            new Model.Item()
                            {
                                name = str.Length > 1 ? str[0] : string.Empty,
                                value = str.Length > 1 ? str[1] : str[0]
                            });
                    }
                }
            return result;
        }

        public static string DeserializeToMessage(this IList<Model.Item> args)
        {
            string result = string.Empty;
            foreach (Model.Item o in args)
            {
                result += (o.name.IsNull() ? o.value : o.name + "=" + o.value) + ";";
            }
            return result;
        }
    }
}
